<template>
    <el-form :model="orderinfo" label-width="75px" style="padding: 10px">
        <el-form-item label="领/退料人">
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请点击搜索选择！" v-model="userinfo.name" :disabled="true">
                        <template slot="prepend">姓名</template>
                        <el-button slot="append" icon="search" @click="getInfo('/selectStaff', 1)">查找</el-button>
                    </el-input>
                </el-col>
            </el-row>
        </el-form-item>
        <el-form-item label="选择产品">
            <el-button type="success" @click="getInfo('/selectGoods')" icon="h-kaidan" class="mb">选择产品</el-button>
            <el-table :data="list" :stripe="true" height="500" show-summary>
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="name" label="商品名称"></el-table-column>
                <el-table-column prop="num" label="数量" width="100">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.num"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button icon="delete2" type="danger" size="mini" @click="deleteInfo(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item label="描述">
            <el-input type="textarea" v-model="orderinfo.remarks"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
            <el-button @click="closewin">取消</el-button>
        </el-form-item>
    </el-form>
</template>
<!-- Placed js at the end of the document so the pages load faster -->
<script type="text/javascript">
import C from '@/common/common.js'

export default {
  name: 'addPickOrder',
  data () {
    return {
      list: [], // 商品列表
      orderinfo: {
        remarks: '',
        ordertype: C.getDatas().ordertype
      },
      alln: 0,
      userinfo: {}
    }
  },
  methods: {
    getInfo: async function (url, t) {
      var that = this
      let data = await C.openBrowserWindow(url, {}, { width: 1000, height: 850, useSpa: false, spaModel: 'shop', transparent: true, frame: false
      })
      if (data) {
        if (t == 1) {
          for (var k = 0; k < data.length; k++) {
            that.userinfo = data[k]
          }
        } else {
          for (var i = data.length - 1; i >= 0; i--) {
            if (!this.inarray(data[i].id, that.list)) {
              let cacheData = {
                id: data[i].id,
                name: data[i].name,
                num: 1
              }
              that.list.push(cacheData)
            }
          }
        }
      }
    },
    deleteInfo: function (index) {
      this.list.splice(index, 1)
    },
    onSubmit: async function () {
      let goback = false
      // 判断是否有领料人
      if (!this.userinfo.id) {
        C.error('请添加' + this.typename + '人！')
        return false
      }
      let len = this.list.length
      if (len < 1) {
        C.error('请添加' + this.typename + '产品！')
        return false
      }
      for (let i = 0; i < len; i++) {
        if (this.list[i].num <= 0) {
          goback = true
        }
      }
      if (goback) {
        C.error(this.typename + '产品数量不能小于或等于零！')
        return false
      }

      let data = {
        list: this.list,
        orderinfo: this.orderinfo,
        userinfo: this.userinfo
      }
      let res = await C.ajax('/shop/stock/addstockorder', { data: JSON.stringify(data) }, 'post')
      if (res) {
        C.closeBrowserWindow(res)
      }
    },
    closewin: function () {
      C.closewin()
    },
    inarray (id, array) {
      let res = false
      for (var i = array.length - 1; i >= 0; i--) {
        if (id == array[i].id) {
          res = true
        }
      }
      return res
    }
  },
  computed: {
    typename: function () {
      return this.orderinfo.ordertype == 8 ? '领料' : '退料'
    }
  }
}
</script>